package com.conjane.algo.queue;

import java.util.ArrayDeque;
import java.util.Queue;

public class RecentCounter {

    private static final int[] array = new int[10000];

    public static int ping(int t){
        int end = 0;
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == 0){
                array[i] = t;
                end = i;
                break;
            }
        }

        for (int i = end; i >= 0; i--) {
            if (array[i] >= t - 3000){
                count++;
            }
        }
        return count;
    }

    private static final Queue<Integer> queue = new ArrayDeque<>();
    public static int pingPlus(int t){
        queue.add(t);
        while (queue.peek().intValue() < t - 3000){
            queue.poll();
        }
        return queue.size();
    }

    public static void main(String[] args) {
        System.out.println(pingPlus(1));
        System.out.println(pingPlus(2));
        System.out.println(pingPlus(3001));
        System.out.println(pingPlus(3002));
    }
}
